<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="600px"
    append-to-body
    :before-close="close"
  >
    <el-form
      :model="editForm"
      ref="editFormRef"
      label-width="80px"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="名称" prop="name">
            <el-input
              v-model="editForm.name"
              placeholder="请输入名称"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label-width="100px" label="类型" prop="syncDataType">
            <el-select v-model="editForm.type" placeholder="请选择类型" maxlength="30">
              <dictionary-option code="syncConditionType"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="100px" label="覆盖" prop="syncDataType">
            <el-select v-model="editForm.overWrite" placeholder="请选择是否覆盖" maxlength="30">
              <dictionary-option code="yesNo"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="表清单" prop="tableList">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.tableStr"
              placeholder="请输入表，逗号分隔"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="全局表" prop="allTableStr">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.allTableStr"
              placeholder="请输入表，逗号分隔"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="租户和全局表" prop="tenantAndGlobalTableStr">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.tenantAndGlobalTableStr"
              placeholder="请输入表，逗号分隔"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="忽略表" prop="ignoreTableStr">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.ignoreTableStr"
              placeholder="请输入表，逗号分隔"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label-width="100px" label="只导入表结构" prop="noDataTableStr">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.noDataTableStr"
              placeholder="请输入表，逗号分隔"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="editForm.type === '2'">
        <el-col :span="24">
          <el-form-item label-width="100px" label="条件" prop="conditionStr">
            <el-input
              type="textarea"
              :rows="3"
              v-model="editForm.conditionStr"
              placeholder="请输入条件"
              width="200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {getCurrentInstance, ref} from 'vue'

import {add, getById, update} from '@/api/sync/condition'

const {proxy} = getCurrentInstance()

const visible = ref(false)
const title = ref('')
const rules = ref({
  name: [{required: true, message: '名称不能为空', trigger: 'blur'}]
})
const editForm = ref({
  id: undefined,
  tableStr: undefined,
  conditionStr: undefined,
  allTableStr: undefined,
  type: '1',
  overWrite: '1',
  tenantAndGlobalTableStr: undefined,
  tenantAndGlobalTable: undefined,
  ignoreTableStr: undefined,
  noDataTableStr: undefined,
  tableList: undefined,
  noDataTable: undefined,
  allTable: undefined,
  ignoreTable: undefined
})

/**
 * 显示弹窗
 */
async function show(id) {
  visible.value = true
  if (id) {
    title.value = '修改条件'
    const {data} = await getById(id)
    editForm.value = data
    console.log('form', editForm.value)
    if (editForm.value.allTable != null && editForm.value.allTable.length > 0) {
      editForm.value.allTableStr = editForm.value.allTable.join()
    }
    if (editForm.value.tableList != null) {
      editForm.value.tableStr = editForm.value.tableList.join()
    }
    if (editForm.value.ignoreTable != null && editForm.value.ignoreTable.length > 0) {
      editForm.value.ignoreTableStr = editForm.value.ignoreTable.join()
    }
    if (editForm.value.tenantAndGlobalTable != null && editForm.value.tenantAndGlobalTable.length > 0) {
      editForm.value.tenantAndGlobalTableStr = editForm.value.tenantAndGlobalTable.join()
    }
    if (editForm.value.noDataTable != null && editForm.value.noDataTable.length > 0) {
      editForm.value.noDataTableStr = editForm.value.noDataTable.join()
    } else {

    }
    if (editForm.value.ignoreTable != null && editForm.value.ignoreTable.length > 0) {
      editForm.value.ignoreTableStr = editForm.value.ignoreTable.join()
    }
    editForm.value.type = editForm.value.type + ''
    editForm.value.overWrite = editForm.value.overWrite + ''
  } else {
    title.value = '新增条件'
    reset()
    editForm.value.id = undefined
  }
}

/**
 * 关闭
 */
function close() {
  visible.value = false
  reset()
}

/**
 * 重置
 */
function reset() {
  proxy.resetForm('editFormRef')
}

/**
 * 提交数据
 */
const emit = defineEmits(null)

function handleSave() {
  proxy.$refs.editFormRef.validate(async (valid) => {
    if (valid) {
      // 修改
      if (editForm.value.tableStr) {
        editForm.value.tableList = editForm.value.tableStr.split(",");
      } else {
        editForm.value.tableList = []
      }
      if (editForm.value.allTableStr) {
        editForm.value.allTable = editForm.value.allTableStr.split(",");
      } else {
        editForm.value.allTable = []
      }
      if (editForm.value.tenantAndGlobalTableStr) {
        editForm.value.tenantAndGlobalTable = editForm.value.tenantAndGlobalTableStr.split(",");
      } else {
        editForm.value.tenantAndGlobalTable = []
      }
      if (editForm.value.noDataTableStr) {
        editForm.value.noDataTable = editForm.value.noDataTableStr.split(",");
      } else {
        editForm.value.noDataTable = []
      }
      if (editForm.value.ignoreTableStr) {
        editForm.value.ignoreTable = editForm.value.ignoreTableStr.split(",");
      } else {
        editForm.value.ignoreTable = []
      }
      if (editForm.value.id) {
        await update(editForm.value);
        proxy.$modal.msgSuccess('修改成功')
      } else {
        // 新增
        await add(editForm.value)
        proxy.$modal.msgSuccess('新增成功')
      }
      emit('fetch-data')
      close()
    }
  })
}

defineExpose({
  show
})
</script>
